import { ref } from 'vue'

export type BizNatureOption = {
	id: number;
	name: string;
}

export function useBizNature() {
	const pickerRef = ref<any>(null)
	const columns = ref<BizNatureOption[]>([])
	const show = ref(false)

	const open = (value: number) => {
		let pickerIndex = 0

		if (value) {
			pickerIndex = columns.value.findIndex((item) => item.id === value)
		}

		pickerRef.value.setIndexs([Math.max(pickerIndex, 0)])

		show.value = true
	}

	const close = () => {
		show.value = false
	}

	const getSelectedName = (id: number) => {
		const cur = columns.value.find((item) => item.id === id)

		return cur?.name
	}

	const getData = () => {
		uni.$u.get<BizNatureOption[]>('/company/selnature').then((res) => {
			if (res.code === 1) {
				columns.value = res.data
			}
		})
	}

	getData()

	return {
		pickerRef,
		columns,
		show,
		open,
		close,
		getSelectedName,
	}
}
